﻿using Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;

namespace Web.Hanlder
{
    public class OrderHandler : IHttpHandler
    {
        #region Base
        HttpRequest Request;
        HttpResponse Response;
        System.Web.SessionState.HttpSessionState Session;
        HttpServerUtility Server;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Request = context.Request;
            Response = context.Response;
            Session = context.Session;
            Server = context.Server;
            string method = Request["Method"].ToString();
            System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(method);
            methodInfo.Invoke(this, null);
        }
        public List<Model.tb_order> GetOBJList()
        {
            return Common.SqlExption.GetList<Model.tb_order>();
        }
        public void ChangeOrder()
        {
            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            string ck = string.Empty;
            if (Request["cks"] == null)
            {
                rh.states = 1;
                rh.msg = "请传入对应参数！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
                return;
            }
            ck = Request["cks"];
            if (Request["cks"].Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = "请选择要结账的订单！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
                return;
            }
            string[] arr = ck.Split(',');
            for (int i = 0; i < arr.Length; i++)
            {
                int oid = 0;
                if (int.TryParse(arr[i], out oid))
                {
                    var model = Common.SqlExption.GetList<Model.tb_order>().FirstOrDefault(c => c.Id.Equals(oid));
                    if (model != null)
                    {
                        if (model.pay_create_time == null || model.pay_create_time.Equals(Convert.ToDateTime("1753-01-01 12:00:00")))
                        {
                            model.pay_create_time = DateTime.Now;
                            Common.SqlExption.Update(model);
                            //给服务器发送
                            #region 给服务器推送监听订单信息
                            Client client = new Client("127.0.0.1", 8080);
                            if (client.Connected)
                            {
                         
                                    client.SendMessage(model.Id.ToString());

                                Thread.Sleep(200);
                            
                            }
                            else
                            {
                                rh.states = 1;
                                rh.msg = "结算服务器未开启，请通知服务商！";
                                string sjson = JsonConvert.SerializeObject(rh);
                                Response.Write(sjson);
                                Response.End();
                                return;

                            }
                            //Console.ReadKey();
                            #endregion
                        }
                        }
                      
                }
            }
            rh.states = 0;

            string ssjson = JsonConvert.SerializeObject(rh);
            Response.Write(ssjson);
            Response.End();
        }
        public void CheckOutOrder()
        {
            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            string ck = string.Empty;
            if (Request["cks"]==null)
            {
                rh.states = 1;
                rh.msg = "请传入对应参数！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
                return;
            }
            ck = Request["cks"];
            if (Request["cks"].Trim().Length<=0)
            {
                 rh.states = 1;
                rh.msg = "请选择要结账的订单！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
                return;
            }
            string[] arr = ck.Split(',');
            for (int i = 0; i < arr.Length; i++)
            {
                int oid = 0;
                if (int.TryParse(arr[i], out oid))
                {
                    var model = Common.SqlExption.GetList<Model.tb_order>().FirstOrDefault(c => c.Id.Equals(oid));
                    if (model!=null)
                    {
                        model.status = 2;
                        Common.SqlExption.Update(model);
                    }
                }
            }
            rh.states = 0;
    
            string sjson = JsonConvert.SerializeObject(rh);
            Response.Write(sjson);
            Response.End();
        }
        public void GetList()
        {
            var data = Common.SqlExption.GetList<Model.view_admin_order>().AsQueryable();
            if (Request["order_number"] != null)
            {
                string txt_search = Request["order_number"];
                data = data.Where(c => c.order_number.Contains(txt_search));
            }
            string result = JsonConvert.SerializeObject(data.ToList());
            Response.Write(result);
            Response.End();

        }
        public void Del()
        {
            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = int.Parse(Request["Id"]);

            var model = GetOBJList().FirstOrDefault(c => c.Id.Equals(Id));
            if (model != null)
            {
                Common.SqlExption.Delete<Model.tb_order>(Id);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();
        }

        public void Dels()
        {
            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            if (string.IsNullOrEmpty(Common.UICookie.ReadCookie("ClientUId")))
            {
                rh.states = 1;
                rh.msg = "请先登录！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
                return;
               
            }
            else
            {
                int cid = Convert.ToInt32(Common.UICookie.ReadCookie("ClientUId"));
                var list = GetOBJList().Where(c => c.uid.Equals(cid) && c.status.Equals(1)).ToList();
                if ( list.Count()>0)
                {
                    for (int i = 0; i < list.Count(); i++)
                    {
                        Common.SqlExption.Delete<Model.tb_order>(list[i].Id);
                    }
                }
               
            }
            rh.states = 0;

            string sjson = JsonConvert.SerializeObject(rh);
            Response.Write(sjson);
            Response.End();
        }
        public void Edit()
        {


            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = Convert.ToInt32(Request["Id"]);
            if (!int.TryParse(Request["Id"], out Id))
            {
                if (Id < 0)
                {
                    rh.states = 1;
                    rh.msg = " 验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                    return;
                }
            }
            var order_number = Request["order_number"];
            if (order_number.Trim().Length <= 0)
            {
                rh.states = 1;
                rh.msg = " 验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
                return;
            }
            int gid = Convert.ToInt32(Request["gid"]);
            if (!int.TryParse(Request["gid"], out gid))
            {
                if (gid < 0)
                {
                    rh.states = 1;
                    rh.msg = "商品编号验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                    return;
                }
            }
            int status = Convert.ToInt32(Request["status"]);
            if (!int.TryParse(Request["status"], out status))
            {
                if (status < 0)
                {
                    rh.states = 1;
                    rh.msg = "状态验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                    return;
                }
            }
            int buy_nums = Convert.ToInt32(Request["buy_nums"]);
            if (!int.TryParse(Request["buy_nums"], out buy_nums))
            {
                if (buy_nums < 0)
                {
                    rh.states = 1;
                    rh.msg = "购买数量验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                    return;
                }
            }
            decimal buy_prices = Convert.ToDecimal(Request["buy_prices"]);
            if (!decimal.TryParse(Request["buy_prices"], out buy_prices))
            {
                if (buy_prices < 0)
                {
                    rh.states = 1;
                    rh.msg = "购买单价验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                    return;
                }
            }
            int uid = Convert.ToInt32(Request["uid"]);
            if (!int.TryParse(Request["uid"], out uid))
            {
                if (uid < 0)
                {
                    rh.states = 1;
                    rh.msg = " 验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                    return;
                }
            }
            var model = Common.SqlExption.GetList<Model.tb_order>().FirstOrDefault(c => c.Id.Equals(Id));
            if (model == null)
            {
                model = new Model.tb_order();
                model.Id = Id;
                model.order_number = order_number;
                model.create_time = DateTime.Now;
                model.gid = gid;
                model.status = status;
                model.buy_nums = buy_nums;
                model.buy_prices = buy_prices;
                model.uid = uid;
                Common.SqlExption.Add(model);
            }
            else
            {
                model.Id = Id;
                model.order_number = order_number;
        
                model.gid = gid;
                model.status = status;
                model.buy_nums = buy_nums;
                model.buy_prices = buy_prices;
                model.uid = uid;
                Common.SqlExption.Update(model);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();

        }
        public void ExShop()
        {
            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = Convert.ToInt32(Request["Id"]);
            if (!int.TryParse(Request["Id"], out Id))
            {
                if (Id < 0)
                {
                    rh.states = 1;
                    rh.msg = " 验证错误！";
                    string json = JsonConvert.SerializeObject(rh);
                    Response.Write(json);
                    Response.End();
                    return;
                }
            }
            var model = Common.SqlExption.GetList<Model.tb_order>().FirstOrDefault(c => c.Id.Equals(Id));
            if (model!=null)
            {
                model.status = 6;
                Common.SqlExption.Update(model);
            }
            else
            {
                rh.states = 1;
                rh.msg = " 模型验证错误！";
                string json = JsonConvert.SerializeObject(rh);
                Response.Write(json);
                Response.End();
                return;
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        #endregion
    }
}
